BUI.use('common/page');
BUI.use([ 'common/search', 'bui/overlay', 'bui/grid', 'bui/data', 'bui/form' ],
function(Search, Overlay, Grid, Data) {
	columns = [
			{
				title : 'KEYS',
				dataIndex : 'keys',
				width : 500
			},
			{
				title : '操作',
				dataIndex : '',
				width : 180,
				renderer : function(value, obj) {
					delKey = '<span class="grid-command btn-delKey" title="删除">删除</span>';
					return delKey;
				}
			} ];

	store = new BUI.Data.Store({
  	  url : 'findAllKey',
	  pageSize : 9999999,
	  remoteSort : true
	 });
	gridCfg = Search.createGridCfg(columns, {
		tbar : {
			items : [ {
				text : '<i class="icon-remove"></i>删除',
				btnCls : 'button button-small',
				handler : delFunction
			}, {
				text : '<i class="icon-refresh"></i>刷新',
				btnCls : 'button button-small',
				handler : function() {
					store.load();
				}
			} ]
		},
		plugins : [ BUI.Grid.Plugins.CheckSelection ]
	// 插件形式引入多选表格

	});

	var search = new Search({
		store : store,
		autoSearch : true,
		gridCfg : gridCfg
	});
	grid = search.get('grid');

	// 删除操作
	function delFunction() {
		var selections = grid.getSelection();
		delItems(selections);
	}

	function delItems(items) {
		var ids = "";
		BUI.each(items, function(item) {
			ids += item.keys + ",";
		});
		if (ids.length) {
			BUI.Message.Confirm('确认要删除选中的记录么？', function() {
				jQuery.ajax({
					type : 'POST',
					url : 'delete',
					dataType : 'json',
					data : {
						'keys' : ids
					},
					complete : function(data) {
						if (data.responseText == "success") { // 删除成功
							search.load();
						} else { // 删除失败
							BUI.Message.Alert('删除失败！');
						}
					}
				});
			}, 'question');
		}
	}
	// 监听事件，删除一条记录
	grid.on('cellclick', function(ev) {
		var sender = jQuery(ev.domTarget); // 点击的Dom
		if (sender.hasClass('btn-del')) {
			var record = ev.record;
			delItems([ record ]);
		}
	});
	grid.on('cellclick', function(ev) {
		var sender = jQuery(ev.domTarget); // 点击的Dom
		if (sender.hasClass('btn-delKey')) {
			var record = ev.record;
			delItems([ record ]);
		}
	});

});